package org.etlt.job;

import org.apache.commons.lang3.StringUtils;
import org.etlt.EtltRuntimeException;
import org.etlt.SettingCheck;
import org.etlt.SettingValidationException;

import java.util.*;

public class JobSetting implements SettingCheck {

    public static final String XML_SUFFIX = ".xml";

    public static final String JSON_SUFFIX = ".json";

    public static final String PROPERTY_PREFIX = "${";

    public static final String PROPERTY_SUFFIX = "}";

    private String name;

    private int parallel = 1;

    private List<String> extractors = new ArrayList<>();

    private List<String> loaders = new ArrayList<>();

    private List<String> checklists = new ArrayList<>();

    private String bundledMapping;

    private List<String> mappings = new ArrayList<>();

    private Map<String, String> properties = new HashMap<>();

    private Set<String> functionTypes = new HashSet<>();

    private List<ResourceSetting> resources = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getExtractors() {
        return extractors;
    }

    public void setExtractors(List<String> extractors) {
        this.extractors = extractors;
    }

    public List<String> getLoaders() {
        return loaders;
    }

    public void setLoaders(List<String> loaders) {
        this.loaders = loaders;
    }

    public List<String> getChecklists() {
        return this.checklists;
    }

    public String getBundledMapping() {
        return bundledMapping;
    }

    public void setBundledMapping(String bundledMapping) {
        this.bundledMapping = bundledMapping;
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public Set<String> getFunctionTypes(){
        return this.functionTypes;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }

    public void setResources(List<ResourceSetting> settings) {
        this.resources = settings;
    }

    public int getParallel() {
        return parallel;
    }

    public void setParallel(int parallel) {
        this.parallel = parallel;
    }

    public List<ResourceSetting> getResources() {
        return this.resources;
    }

    public List<String> getMappings() {
        return mappings;
    }

    public void setMappings(List<String> mappings) {
        this.mappings = mappings;
    }

    @Override
    public void check() {
        if (StringUtils.isBlank(getName())) throw new SettingValidationException("missing name.");
        if (getParallel() < 1) throw new SettingValidationException("parallel must greater than 0: " + getParallel());
    }

    public String getPropertyValue(String name) {
        if (name.startsWith(PROPERTY_PREFIX) && name.endsWith(PROPERTY_SUFFIX)) {
            String propertyName = name.substring(PROPERTY_PREFIX.length(), name.length() - PROPERTY_SUFFIX.length());
            String value = getProperties().get(propertyName);
            if (value == null) throw new EtltRuntimeException("no property found for: " + propertyName);
            return value;
        }
        return name;
    }

    public static boolean isXml(String filename) {
        return filename.toLowerCase().endsWith(XML_SUFFIX);
    }

    public static boolean isJson(String filename) {
        return filename.toLowerCase().endsWith(JSON_SUFFIX);
    }
}
